home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / linux / atari / source / source.lzh / atari-linux-0.01pl3 / fs / ext2 / ialloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  17.7 KB  |  637 lines

  1.  
  2. /*
  3.  *  linux/fs/ext2/ialloc.c
  4.  *
  5.  *  Copyright (C) 1992, 1993  Remy Card (card@masi.ibp.fr)
  6.  *
  7.  *  BSD ufs-inspired inode and directory allocation by 
  8.  *  Stephen Tweedie (sct@dcs.ed.ac.uk), 1993
  9.  */
  10.  
  11. /* ialloc.c contains the inodes allocation and deallocation routines */
  12.  
  13. /*
  14.  
  15.    The free inodes are managed by bitmaps.  A file system contains several
  16.    blocks groups.  Each group contains 1 bitmap block for blocks, 1 bitmap
  17.    block for inodes, N blocks for the inode table and data blocks.
  18.  
  19.    The file system contains group descriptors which are located after the
  20.    super block.  Each descriptor contains the number of the bitmap block and
  21.    the free blocks count in the block.  The descriptors are loaded in memory
  22.    when a file system is mounted (see ext2_read_super).
  23.  
  24. */
  25.  
  26. #include <linux/fs.h>
  27. #include <linux/ext2_fs.h>
  28. #include <linux/kernel.h>
  29. #include <linux/sched.h>
  30. #include <linux/stat.h>
  31. #include <linux/string.h>
  32. #include <linux/locks.h>
  33.  
  34. #include <asm/bitops.h>
  35.  
  36. #if defined(__i386__)
  37. static inline int find_first_zero_bit (unsigned long * addr, unsigned size)
  38. {
  39.     int res;
  40.  
  41.     if (!size)
  42.         return 0;
  43.     __asm__("
  44.         cld
  45.         movl $-1,%%eax
  46.         repe; scasl
  47.         je 1f
  48.         subl $4,%%edi
  49.         movl (%%edi),%%eax
  50.         notl %%eax
  51.         bsfl %%eax,%%edx
  52.         jmp 2f
  53. 1:        xorl %%edx,%%edx
  54. 2:        subl %%ebx,%%edi
  55.         shll $3,%%edi
  56.         addl %%edi,%%edx"
  57.         : "=d" (res)
  58.         : "c" ((size + 31) >> 5), "D" (addr), "b" (addr)
  59.         : "ax", "bx", "cx", "di");
  60.     return res;
  61. }
  62. #elif defined(__mc68000__)
  63. static inline int find_first_zero_bit(unsigned long * addr, unsigned size)
  64. {
  65.     unsigned long res;                
  66.     unsigned long *p;                
  67.  
  68.     if (!size)
  69.         return 0;
  70.     __asm__ __volatile__ ("    moveq #-1,d0\n\t"
  71.                   "1:"
  72.                   "    cmpl  %1@+,d0\n\t"
  73.                   "    bne   2f\n\t"
  74.                   "    subql #1,%0\n\t"
  75.                   "    bne   1b\n\t"
  76.                   "    bra   5f\n\t"
  77.                   "2:"
  78.                   "    movel %1@-,d0\n\t"
  79.                   "    notl  d0\n\t"
  80.                   "    bfffo d0{#0,#0},%0\n\t"
  81.                   "5:"
  82.                   : "=d" (res), "=a" (p)
  83.                   : "0" ((size + 31) >> 5), "1" (addr)
  84.                   : "d0");
  85.     return ((p - addr) << 5) + res;
  86. }
  87. #endif
  88.  
  89. static void read_inode_bitmap (struct super_block * sb,
  90.                    unsigned long block_group,
  91.                    unsigned int bitmap_nr)
  92. {
  93.     unsigned long group_desc;
  94.     unsigned long desc;
  95.     struct ext2_group_desc * gdp;
  96.     struct buffer_head * bh;
  97.  
  98.     group_desc = block_group / EXT2_DESC_PER_BLOCK(sb);
  99.     desc = block_group % EXT2_DESC_PER_BLOCK(sb);
  100.     if (!sb->u.ext2_sb.s_group_desc[group_desc])
  101.         ext2_panic (sb, "read_inode_bitmap",
  102.                 "Group descriptor not loaded\n"
  103.                 "block_group = %lu, group_desc = %lu, desc = %lu",
  104.                 block_group, group_desc, desc);
  105.     gdp = (struct ext2_group_desc *) sb->u.ext2_sb.s_group_desc[group_desc]->b_data;
  106.     bh = bread (sb->s_dev, gdp[desc].bg_inode_bitmap, sb->s_blocksize);
  107.     if (!bh)
  108.         ext2_panic (sb, "read_inode_bitmap", "Cannot read inode bitmap\n"
  109.                 "block_group = %lu, group_desc = %lu, desc = %lu, inode_bitmap = %lu",
  110.                 block_group, group_desc, desc, gdp[desc].bg_inode_bitmap);
  111.     sb->u.ext2_sb.s_inode_bitmap_number[bitmap_nr] = block_group;
  112.     sb->u.ext2_sb.s_inode_bitmap[bitmap_nr] = bh;
  113. }
  114.  
  115. /*
  116.  * load_inode_bitmap loads the inode bitmap for a blocks group
  117.  *
  118.  * It maintains a cache for the last bitmaps loaded.  This cache is managed
  119.  * with a LRU algorithm.
  120.  *
  121.  * Notes:
  122.  * 1/ There is one cache per mounted file system.
  123.  * 2/ If the file system contains less than EXT2_MAX_GROUP_LOADED groups,
  124.  *    this function reads the bitmap without maintaining a LRU cache.
  125.  */
  126. static int load_inode_bitmap (struct super_block * sb,
  127.                   unsigned int block_group)
  128. {
  129.     int i, j;
  130.     unsigned long inode_bitmap_number;
  131.     struct buffer_head * inode_bitmap;
  132.  
  133.     if (block_group >= sb->u.ext2_sb.s_groups_count)
  134.         ext2_panic (sb, "load_inode_bitmap",
  135.                 "block_group >= groups_count\n"
  136.                 "block_group = %d, groups_count = %lu",
  137.                  block_group, sb->u.ext2_sb.s_groups_count);
  138.     if (sb->u.ext2_sb.s_loaded_inode_bitmaps > 0 &&
  139.         sb->u.ext2_sb.s_inode_bitmap_number[0] == block_group)
  140.         return 0;
  141.     if (sb->u.ext2_sb.s_groups_count <= EXT2_MAX_GROUP_LOADED) {
  142.         if (sb->u.ext2_sb.s_inode_bitmap[block_group]) {
  143.             if (sb->u.ext2_sb.s_inode_bitmap_number[block_group] != block_group)
  144.                 ext2_panic (sb, "load_inode_bitmap",
  145.                         "block_group != inode_bitmap_number");
  146.             else
  147.                 return block_group;
  148.         } else {
  149.             read_inode_bitmap (sb, block_group, block_group);
  150.             return block_group;
  151.         }
  152.     }
  153.  
  154.     for (i = 0; i < sb->u.ext2_sb.s_loaded_inode_bitmaps &&
  155.             sb->u.ext2_sb.s_inode_bitmap_number[i] != block_group;
  156.          i++)
  157.         ;
  158.     if (i < sb->u.ext2_sb.s_loaded_inode_bitmaps &&
  159.           sb->u.ext2_sb.s_inode_bitmap_number[i] == block_group) {
  160.         inode_bitmap_number = sb->u.ext2_sb.s_inode_bitmap_number[i];
  161.         inode_bitmap = sb->u.ext2_sb.s_inode_bitmap[i];
  162.         for (j = i; j > 0; j--) {
  163.             sb->u.ext2_sb.s_inode_bitmap_number[j] =
  164.                 sb->u.ext2_sb.s_inode_bitmap_number[j - 1];
  165.             sb->u.ext2_sb.s_inode_bitmap[j] =
  166.                 sb->u.ext2_sb.s_inode_bitmap[j - 1];
  167.         }
  168.         sb->u.ext2_sb.s_inode_bitmap_number[0] = inode_bitmap_number;
  169.         sb->u.ext2_sb.s_inode_bitmap[0] = inode_bitmap;
  170.     } else {
  171.         if (sb->u.ext2_sb.s_loaded_inode_bitmaps < EXT2_MAX_GROUP_LOADED)
  172.             sb->u.ext2_sb.s_loaded_inode_bitmaps++;
  173.         else
  174.             brelse (sb->u.ext2_sb.s_inode_bitmap[EXT2_MAX_GROUP_LOADED - 1]);
  175.         for (j = sb->u.ext2_sb.s_loaded_inode_bitmaps - 1; j > 0; j--) {
  176.             sb->u.ext2_sb.s_inode_bitmap_number[j] =
  177.                 sb->u.ext2_sb.s_inode_bitmap_number[j - 1];
  178.             sb->u.ext2_sb.s_inode_bitmap[j] =
  179.                 sb->u.ext2_sb.s_inode_bitmap[j - 1];
  180.         }
  181.         read_inode_bitmap (sb, block_group, 0);
  182.     }
  183.     return 0;
  184. }
  185.  
  186. /*
  187.  * This function sets the deletion time for the inode
  188.  *
  189.  * This may be used one day by an 'undelete' program
  190.  */
  191. static void set_inode_dtime (struct inode * inode,
  192.                  struct ext2_group_desc * gdp, unsigned long desc)
  193. {
  194.     unsigned long inode_block;
  195.     struct buffer_head * bh;
  196.     struct ext2_inode * raw_inode;
  197.  
  198.     inode_block = gdp[desc].bg_inode_table + (((inode->i_ino - 1) %
  199.             EXT2_INODES_PER_GROUP(inode->i_sb)) /
  200.             EXT2_INODES_PER_BLOCK(inode->i_sb));
  201.     bh = bread (inode->i_sb->s_dev, inode_block, inode->i_sb->s_blocksize);
  202.     if (!bh)
  203.         ext2_panic (inode->i_sb, "set_inode_dtime",
  204.                 "Cannot load inode table block\n"
  205.                 "inode=%lu, inode_block=%lu",
  206.                 inode->i_ino, inode_block);
  207.     raw_inode = ((struct ext2_inode *) bh->b_data) +
  208.             (((inode->i_ino - 1) %
  209.             EXT2_INODES_PER_GROUP(inode->i_sb)) %
  210.             EXT2_INODES_PER_BLOCK(inode->i_sb));
  211.     raw_inode->i_links_count = 0;
  212.     raw_inode->i_dtime = CURRENT_TIME;
  213.     bh->b_dirt = 1;
  214.     if (IS_SYNC(inode)) {
  215.         ll_rw_block (WRITE, 1, &bh);
  216.         wait_on_buffer (bh);
  217.     }
  218.     brelse (bh);
  219. }
  220.  
  221. void ext2_free_inode (struct inode * inode)
  222. {
  223.     struct super_block * sb;
  224.     struct buffer_head * bh;
  225.     struct buffer_head * bh2;
  226.     unsigned long block_group;
  227.     unsigned long bit;
  228.     unsigned long group_desc;
  229.     unsigned long desc;
  230.     int bitmap_nr;
  231.     struct ext2_group_desc * gdp;
  232.     struct ext2_super_block * es;
  233.  
  234.     if (!inode)
  235.         return;
  236.     if (!inode->i_dev) {
  237.         printk ("ext2_free_inode: inode has no device\n");
  238.         return;
  239.     }
  240.     if (inode->i_count > 1) {
  241.         printk ("ext2_free_inode: inode has count=%d\n",
  242.             inode->i_count);
  243.         return;
  244.     }
  245.     if (inode->i_nlink) {
  246.         printk ("ext2_free_inode: inode has nlink=%d\n",
  247.             inode->i_nlink);
  248.         return;
  249.     }
  250.     if (!inode->i_sb) {
  251.         printk("ext2_free_inode: inode on nonexistent device\n");
  252.         return;
  253.     }
  254.  
  255.     ext2_debug ("freeing inode %lu\n", inode->i_ino);
  256.  
  257.     sb = inode->i_sb;
  258.     lock_super (sb);
  259.     if (inode->i_ino < EXT2_FIRST_INO ||
  260.         inode->i_ino > sb->u.ext2_sb.s_es->s_inodes_count) {
  261.         ext2_error (sb, "free_inode",
  262.                 "reserved inode or nonexistent inode");
  263.         unlock_super (sb);
  264.         return;
  265.     }
  266.     es = sb->u.ext2_sb.s_es;
  267.     block_group = (inode->i_ino - 1) / EXT2_INODES_PER_GROUP(sb);
  268.     bit = (inode->i_ino - 1) % EXT2_INODES_PER_GROUP(sb);
  269.     bitmap_nr = load_inode_bitmap (sb, block_group);
  270.     bh = sb->u.ext2_sb.s_inode_bitmap[bitmap_nr];
  271.     if (!bh)
  272.         ext2_panic (sb, "ext2_free_inode",
  273.                 "Unable to load bitmap for group %lu", block_group);
  274.     if (!clear_bit (bit, bh->b_data))
  275.         ext2_warning (sb, "ext2_free_inode",
  276.                   "bit already cleared for inode %lu", inode->i_ino);
  277.     else {
  278.         group_desc = block_group / EXT2_DESC_PER_BLOCK(sb);
  279.         desc = block_group % EXT2_DESC_PER_BLOCK(sb);
  280.         bh2 = sb->u.ext2_sb.s_group_desc[group_desc];
  281.         if (!bh2)
  282.             ext2_panic (sb, "ext2_free_inode",
  283.                     "Group descriptor not loaded for group %lu",
  284.                     group_desc);
  285.         gdp = (struct ext2_group_desc *) bh2->b_data;
  286.         gdp[desc].bg_free_inodes_count++;
  287.         if (S_ISDIR(inode->i_mode))
  288.             gdp[desc].bg_used_dirs_count--;
  289.         bh2->b_dirt = 1;
  290.         es->s_free_inodes_count++;
  291.         sb->u.ext2_sb.s_sbh->b_dirt = 1;
  292.         set_inode_dtime (inode, gdp, desc);
  293.     }
  294.     bh->b_dirt = 1;
  295.     if (sb->s_flags & MS_SYNC) {
  296.         ll_rw_block (WRITE, 1, &bh);
  297.         wait_on_buffer (bh);
  298.     }
  299.  
  300.     sb->s_dirt = 1;
  301.     clear_inode (inode);
  302.     unlock_super (sb);
  303. }
  304.  
  305. /*
  306.  * This function increments the inode version number
  307.  *
  308.  * This may be used one day by the NFS server
  309.  */
  310. static void inc_inode_version (struct inode * inode,
  311.                    struct ext2_group_desc *gdp,
  312.                    int mode)
  313. {
  314.     unsigned long inode_block;
  315.     struct buffer_head * bh;
  316.     struct ext2_inode * raw_inode;
  317.  
  318.     inode_block = gdp->bg_inode_table + (((inode->i_ino - 1) %
  319.             EXT2_INODES_PER_GROUP(inode->i_sb)) /
  320.             EXT2_INODES_PER_BLOCK(inode->i_sb));
  321.     bh = bread (inode->i_sb->s_dev, inode_block, inode->i_sb->s_blocksize);
  322.     if (!bh) {
  323.         ext2_error (inode->i_sb, "inc_inode_version",
  324.                 "Cannot load inode table block"
  325.                 "inode=%lu, inode_block=%lu\n",
  326.                 inode->i_ino, inode_block);
  327.         inode->u.ext2_i.i_version = 1;
  328.         return;
  329.     }
  330.     raw_inode = ((struct ext2_inode *) bh->b_data) +
  331.             (((inode->i_ino - 1) %
  332.             EXT2_INODES_PER_GROUP(inode->i_sb)) %
  333.             EXT2_INODES_PER_BLOCK(inode->i_sb));
  334.     raw_inode->i_version++;
  335.     inode->u.ext2_i.i_version = raw_inode->i_version;
  336.     bh->b_dirt = 1;
  337.     brelse (bh);
  338. }
  339.  
  340. static struct ext2_group_desc * get_group_desc (struct super_block * sb,
  341.                             int group)
  342. {
  343.     struct ext2_group_desc * gdp;
  344.  
  345.     if (group >= sb->u.ext2_sb.s_groups_count || group < 0 )
  346.         ext2_panic (sb, "get_group_desc", "Invalid group %d", group);
  347.     if (!sb->u.ext2_sb.s_group_desc[group / EXT2_DESC_PER_BLOCK(sb)])
  348.         ext2_panic (sb, "get_group_desc",
  349.                 "Descriptor not loaded for group %d", group);
  350.     gdp = (struct ext2_group_desc *)
  351.         sb->u.ext2_sb.s_group_desc[group / EXT2_DESC_PER_BLOCK(sb)]
  352.         ->b_data;
  353.     return gdp + (group % EXT2_DESC_PER_BLOCK(sb));
  354. }
  355.     
  356. /*
  357.  * There are two policies for allocating an inode.  If the new inode is
  358.  * a directory, then a forward search is made for a block group with both
  359.  * free space and a low directory-to-inode ratio; if that fails, then of
  360.  * the groups with above-average free space, that group with the fewest
  361.  * directories already is chosen.
  362.  *
  363.  * For other inodes, search forward from the parent directory\'s block
  364.  * group to find a free inode.
  365.  */
  366. struct inode * ext2_new_inode (const struct inode * dir, int mode)
  367. {
  368.     struct super_block * sb;
  369.     struct buffer_head * bh;
  370.     int i, j, avefreei;
  371.     struct inode * inode;
  372.     int bitmap_nr;
  373.     struct ext2_group_desc * gdp, * tmp;
  374.     struct ext2_super_block * es;
  375.  
  376.     if (!dir || !(inode = get_empty_inode ()))
  377.         return NULL;
  378.     sb = dir->i_sb;
  379.     inode->i_sb = sb;
  380.     inode->i_flags = sb->s_flags;
  381.     lock_super (sb);
  382.     es = sb->u.ext2_sb.s_es;
  383. repeat:
  384.     gdp = NULL; i=0;
  385.     
  386.     if (S_ISDIR(mode)) {
  387.         avefreei = es->s_free_inodes_count /
  388.             sb->u.ext2_sb.s_groups_count;
  389. /* I am not yet convinced that this next bit is necessary.
  390.         i = dir->u.ext2_i.i_block_group;
  391.         for (j = 0; j < sb->u.ext2_sb.s_groups_count; j++) {
  392.             tmp = get_group_desc (sb, i);
  393.             if ((tmp->bg_used_dirs_count << 8) < 
  394.                 tmp->bg_free_inodes_count) {
  395.                 gdp = tmp;
  396.                 break;
  397.             }
  398.             else
  399.             i = ++i % sb->u.ext2_sb.s_groups_count;
  400.         }
  401. */
  402.         if (!gdp) {
  403.             for (j = 0; j < sb->u.ext2_sb.s_groups_count; j++) {
  404.                 tmp = get_group_desc (sb, j);
  405.                 if (tmp->bg_free_inodes_count &&
  406.                     tmp->bg_free_inodes_count >= avefreei) {
  407.                     if (!gdp || 
  408.                         (tmp->bg_free_inodes_count >
  409.                          gdp->bg_free_inodes_count)) {
  410.                         i = j;
  411.                         gdp = tmp;
  412.                     }
  413.                 }
  414.             }
  415.         }
  416.     }
  417.     else 
  418.     { /* Try to place the inode in it\'s parent directory */
  419.         i = dir->u.ext2_i.i_block_group;
  420.         tmp = get_group_desc (sb, i);
  421.         if (tmp->bg_free_inodes_count)
  422.             gdp = tmp;
  423.         else
  424.         { /* Use a quadratic hash to find a group with a free inode */
  425.             for (j = 1; j < sb->u.ext2_sb.s_groups_count; j <<= 1) {
  426.                 i += j;
  427.                 if (i >= sb->u.ext2_sb.s_groups_count)
  428.                     i -= sb->u.ext2_sb.s_groups_count;
  429.                 tmp = get_group_desc (sb, i);
  430.                 if (tmp->bg_free_inodes_count) {
  431.                     gdp = tmp;
  432.                     break;
  433.                 }
  434.             }
  435.         }
  436.         if (!gdp) {
  437.             /* That failed: try linear search for a free inode */
  438.             i = dir->u.ext2_i.i_block_group + 2;
  439.             for (j = 2; j < sb->u.ext2_sb.s_groups_count; j++) {
  440.                 if (++i >= sb->u.ext2_sb.s_groups_count)
  441.                     i = 0;
  442.                 tmp = get_group_desc (sb,i);
  443.                 if (tmp->bg_free_inodes_count) {
  444.                     gdp = tmp;
  445.                     break;
  446.                 }
  447.             }
  448.         }
  449.     }
  450.     
  451.     if (!gdp) {
  452.         unlock_super (sb);
  453.         iput(inode);
  454.         return NULL;
  455.     }
  456.     bitmap_nr = load_inode_bitmap (sb, i);
  457.     bh = sb->u.ext2_sb.s_inode_bitmap[bitmap_nr];
  458.     if (!bh)
  459.         ext2_panic (sb, "ext2_new_inode",
  460.                 "Unable to load bitmap for group %d", i);
  461.     if ((j = find_first_zero_bit ((unsigned long *) bh->b_data,
  462.                       EXT2_INODES_PER_GROUP(sb))) <
  463.         EXT2_INODES_PER_GROUP(sb)) {
  464.         if (set_bit (j, bh->b_data)) {
  465.             ext2_warning (sb, "ext2_new_inode",
  466.                       "bit already set for inode %d", j);
  467.             goto repeat;
  468.         }
  469.         bh->b_dirt = 1;
  470.         if (sb->s_flags & MS_SYNC) {
  471.             ll_rw_block (WRITE, 1, &bh);
  472.             wait_on_buffer (bh);
  473.         }
  474.     } else
  475.         goto repeat;
  476.     j += i * EXT2_INODES_PER_GROUP(sb) + 1;
  477.     if (j > es->s_inodes_count) {
  478.         ext2_error (sb, "ext2_new_inode",
  479.                 "inode > inodes count\n"
  480.                 "block_group = %d,inode=%d", i, j);
  481.         unlock_super (sb);
  482.         iput (inode);
  483.         return NULL;
  484.     }
  485.     gdp->bg_free_inodes_count--;
  486.     if (S_ISDIR(mode))
  487.         gdp->bg_used_dirs_count++;
  488.     sb->u.ext2_sb.s_group_desc[i / EXT2_DESC_PER_BLOCK(sb)]->b_dirt = 1;
  489.     es->s_free_inodes_count--;
  490.     sb->u.ext2_sb.s_sbh->b_dirt = 1;
  491.     sb->s_dirt = 1;
  492.     inode->i_mode = mode;
  493.     inode->i_sb = sb;
  494.     inode->i_count = 1;
  495.     inode->i_nlink = 1;
  496.     inode->i_dev = sb->s_dev;
  497.     inode->i_uid = current->euid;
  498.     inode->i_gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->egid;
  499.     inode->i_dirt = 1;
  500.     inode->i_ino = j;
  501.     inode->i_blksize = sb->s_blocksize;
  502.     inode->i_blocks = 0;
  503.     inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  504.     inode->u.ext2_i.i_flags = dir->u.ext2_i.i_flags;
  505.     inode->u.ext2_i.i_faddr = 0;
  506.     inode->u.ext2_i.i_frag = 0;
  507.     inode->u.ext2_i.i_fsize = 0;
  508.     inode->u.ext2_i.i_file_acl = 0;
  509.     inode->u.ext2_i.i_dir_acl = 0;
  510.     inode->u.ext2_i.i_dtime = 0;
  511.     inode->u.ext2_i.i_block_group = i;
  512.     inode->i_op = NULL;
  513.     if (inode->u.ext2_i.i_flags & EXT2_SYNC_FL)
  514.         inode->i_flags |= MS_SYNC;
  515.     insert_inode_hash(inode);
  516.     inc_inode_version (inode, gdp, mode);
  517.  
  518.     ext2_debug ("allocating inode %lu\n", inode->i_ino);
  519.  
  520.     unlock_super (sb);
  521.     return inode;
  522. }
  523.  
  524. unsigned long ext2_count_free_inodes (struct super_block * sb)
  525. {
  526. #ifdef EXT2FS_DEBUG
  527.     struct ext2_super_block * es;
  528.     unsigned long desc_count, bitmap_count, x;
  529.     unsigned long group_desc;
  530.     unsigned long desc;
  531.     int bitmap_nr;
  532.     struct ext2_group_desc * gdp;
  533.     int i;
  534.  
  535.     lock_super (sb);
  536.     es = sb->u.ext2_sb.s_es;
  537.     desc_count = 0;
  538.     bitmap_count = 0;
  539.     group_desc = 0;
  540.     desc = 0;
  541.     gdp = NULL;
  542.     for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++) {
  543.         if (!gdp) {
  544.             if (!sb->u.ext2_sb.s_group_desc[group_desc]) {
  545.                 printk ("ext2_count_free_inodes: Descriptor not loaded\n");
  546.                 break;
  547.             }
  548.             gdp = (struct ext2_group_desc *) sb->u.ext2_sb.s_group_desc[group_desc]->b_data;
  549.         }
  550.         desc_count += gdp[desc].bg_free_inodes_count;
  551.         bitmap_nr = load_inode_bitmap (sb, i);
  552.         if (sb->u.ext2_sb.s_inode_bitmap[bitmap_nr])
  553.             x = ext2_count_free (sb->u.ext2_sb.s_inode_bitmap[bitmap_nr],
  554.                            EXT2_INODES_PER_GROUP(sb) / 8);
  555.         else {
  556.             x = 0;
  557.             printk ("Cannot load inode bitmap for group %d (bitmap = %d)\n",
  558.                 i, bitmap_nr);
  559.         }
  560.         printk ("group %d: stored = %d, counted = %lu\n",
  561.             i, gdp[desc].bg_free_inodes_count, x);
  562.         bitmap_count += x;
  563.         desc++;
  564.         if (desc == EXT2_DESC_PER_BLOCK(sb)) {
  565.             group_desc++;
  566.             desc = 0;
  567.             gdp = NULL;
  568.         }
  569.     }
  570.     printk("ext2_count_free_inodes: stored = %lu, computed = %lu, %lu\n",
  571.         es->s_free_inodes_count, desc_count, bitmap_count);
  572.     unlock_super (sb);
  573.     return desc_count;
  574. #else
  575.     return sb->u.ext2_sb.s_es->s_free_inodes_count;
  576. #endif
  577. }
  578.  
  579. void ext2_check_inodes_bitmap (struct super_block * sb)
  580. {
  581.     struct ext2_super_block * es;
  582.     unsigned long desc_count, bitmap_count, x;
  583.     unsigned long group_desc;
  584.     unsigned long desc;
  585.     int bitmap_nr;
  586.     struct ext2_group_desc * gdp;
  587.     int i;
  588.  
  589.     lock_super (sb);
  590.     es = sb->u.ext2_sb.s_es;
  591.     desc_count = 0;
  592.     bitmap_count = 0;
  593.     group_desc = 0;
  594.     desc = 0;
  595.     gdp = NULL;
  596.     for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++) {
  597.         if (!gdp) {
  598.             if (!sb->u.ext2_sb.s_group_desc[group_desc]) {
  599.                 ext2_error (sb, "ext2_check_inodes_bitmap",
  600.                         "Descriptor not loaded for group %d",
  601.                         i);
  602.                 break;
  603.             }
  604.             gdp = (struct ext2_group_desc *) sb->u.ext2_sb.s_group_desc[group_desc]->b_data;
  605.         }
  606.         desc_count += gdp[desc].bg_free_inodes_count;
  607.         bitmap_nr = load_inode_bitmap (sb, i);
  608.         if (sb->u.ext2_sb.s_inode_bitmap[bitmap_nr])
  609.             x = ext2_count_free (sb->u.ext2_sb.s_inode_bitmap[bitmap_nr],
  610.                            EXT2_INODES_PER_GROUP(sb) / 8);
  611.         else {
  612.             x = 0;
  613.             ext2_error (sb, "ext2_check_inodes_bitmap",
  614.                     "Cannot load bitmap for group %d (bitmap = %d)",
  615.                     i, bitmap_nr);
  616.         }
  617.         if (gdp[desc].bg_free_inodes_count != x)
  618.             ext2_error (sb, "ext2_check_inodes_bitmap",
  619.                     "Wrong free inodes count in group %d, "
  620.                     "stored = %d, counted = %lu", i,
  621.                     gdp[desc].bg_free_inodes_count, x);
  622.         bitmap_count += x;
  623.         desc++;
  624.         if (desc == EXT2_DESC_PER_BLOCK(sb)) {
  625.             group_desc++;
  626.             desc = 0;
  627.             gdp = NULL;
  628.         }
  629.     }
  630.     if (es->s_free_inodes_count != bitmap_count)
  631.         ext2_error (sb, "ext2_check_inodes_bitmap",
  632.                 "Wrong free inodes count in super block, "
  633.                 "stored = %lu, counted = %lu",
  634.                 es->s_free_inodes_count, bitmap_count);
  635.     unlock_super (sb);
  636. }
  637.